package edu.cicese.alz;

import com.toedter.calendar.JCalendar;
import edu.cicese.alz.swing.dialogs.CustomDialog;
import edu.cicese.alz.swing.SwingUtil;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Date;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 26/06/12
 * Time: 03:18 PM
 */
public class DialogCalendar extends CustomDialog {

	private JCalendar calendar;
	private String dateStr;
	private Date date;

	public DialogCalendar(JFrame owner, String dateStr) {
		super(owner, "Select date", true);

		this.dateStr = dateStr;
		try {
			this.date = SwingUtil.calendarDateFormat.parse(dateStr);
		} catch (ParseException e) { /*ignored*/ }

		setMin(200, 300);
		setInitialLocation();
		setResizable(false);

		initComponents();
	}

	private void initComponents() {
		calendar = new JCalendar();
		calendar.setWeekOfYearVisible(false);
		calendar.setBackground(Color.RED);
		calendar.setDecorationBackgroundColor(Color.RED);

		Date d = new Date();
		try {
			d = SwingUtil.calendarDateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.setDate(d);
//		calendar.addPropertyChangeListener(new PropertyChangeListener() {
//			public void propertyChange(PropertyChangeEvent e) {
//				System.out.println(e.getPropertyName());
//				if ("calendar".equals(e.getPropertyName())) {
//					System.out.printf("Changed dateStr.");
//				}
//			}
//		});

		JButton btnOK = SwingUtil.createButtonBackground("OK", 71, 22, null,
						getIcon("bg"), getIcon("bgO"), getIcon("bgP"));
		btnOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionOK();
			}
		});
		JButton btnCancel = SwingUtil.createButtonBackground("Cancel", 71, 22, null,
								getIcon("bg"), getIcon("bgO"), getIcon("bgP"));
				btnOK.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						actionCancel();
					}
				});

		Box boxButton = Box.createHorizontalBox();
		boxButton.add(Box.createHorizontalGlue());
		boxButton.add(btnOK);
		boxButton.add(Box.createHorizontalStrut(10));
		boxButton.add(btnCancel);
		boxButton.add(Box.createHorizontalStrut(10));

		DateChooser dateChooser = new DateChooser(dateStr, "dd-MM-yyyy");

//		JDateChooser dateChooser = new JDateChooser();
//		dateChooser.setLocale(Locale.ENGLISH);
//		dateChooser.setDateFormatString("dd-MM-yyyy");
//		dateChooser.setPreferredSize(new Dimension(50, 20));
//		dateChooser.setMaximumSize(new Dimension(100, 20));
//
//		JButton calendarButton = dateChooser.getCalendarButton();
//		calendarButton.setIcon(getIcon("calendar_small"));
//		calendarButton.setBackground(Color.WHITE);
//		calendarButton.setBorder(BorderFactory.createLineBorder(SwingUtil.cRolloverBorder));

	/*	Component[] components = dateChooser.getComponents();
		for (Component component : components) {
			if (component instanceof JTextFieldDateEditor) {
				JTextFieldDateEditor textFieldDateEditor = ((JTextFieldDateEditor) component);
				textFieldDateEditor.setBorder(SwingUtil.borderTextField);
			}
		}

		Component[] componentsCalendar = dateChooser.getJCalendar().getComponents();
		for (Component component : componentsCalendar) {
			if (component instanceof JPanel) {
				for (Component componentPanel : ((JPanel) component).getComponents()) {
					if (componentPanel instanceof JPanel) {
						for (Component componentPanel2 : ((JPanel) componentPanel).getComponents()) {
							System.out.println(componentPanel2.toString());
//							if (componentPanel2 instanceof JDayChooser) {
//							}
						}
					}
				}
			}
		}*/

		Box boxMain = Box.createVerticalBox();
		boxMain.add(Box.createVerticalStrut(5));
		boxMain.add(calendar);
		boxMain.add(Box.createVerticalStrut(10));
		boxMain.add(boxButton);
		boxMain.add(Box.createVerticalStrut(10));
		boxMain.add(dateChooser);

		JPanel pContent = new JPanel();
		pContent.setBackground(Color.WHITE);
		pContent.add(boxMain);

		setContentPane(pContent);
	}

	public String getDate() {
		return SwingUtil.calendarDateFormat.format(date);
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void actionOK() {
		setDate(calendar.getDate());
		setVisible(false);
		dispose();
	}

	private void actionCancel() {
		setVisible(false);
				dispose();
	}

	public static void main(String[] a) {
		new DialogCalendar(new JFrame(), "12-07-2012").setVisible(true);
	}

	/*public String showDialog() {
	    setVisible(true);
	    return calendar.getDate() + "";
	}*/
}
